/**
 *  This file is part of OhSnap.
 *
 * 	Copyright (c) 2009 Gabriel Mendonça and Marcelo Machado
 * 
 *  OhSnap is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OhSnap is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OhSnap.  If not, see <http://www.gnu.org/licenses/>.
 *  
 */
package br.mm.cg.particles;

import java.util.LinkedList;
import java.util.List;

import processing.core.PGraphics;
import processing.core.PVector;

/**
 * Represents an exploding particle Generator. Its particles will travel away
 * from its center.
 * 
 * @author mendonça
 * 
 */
public class Explosion extends Generator {

	private static final float EXPLOSION_RADIUS = 450.0f;
	private static final float ACCELERATION = 0.1f;

	/**
	 * Constructor.
	 */
	public Explosion(Generator generator) {
		super(generator.parent, generator.position, generator.capacity,
				generator.max, generator.min, generator.particles);

		this.color = generator.color;
		this.destination = generator.destination;
		this.multiplier = generator.multiplier;
		this.speed = generator.speed;
		this.radius = EXPLOSION_RADIUS;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.mm.cg.particles.Generator#draw()
	 */
	@Override
	public void draw(PGraphics pGraphics) {
		if (this.particles.size() > 0) {
			List<Particle> newList = new LinkedList<Particle>();

			pGraphics.pushMatrix();
			pGraphics.translate(position.x, position.y, position.z);

			for (Particle p : particles) {
				if (p.position.mag() < this.radius) {
					PVector displacement = PVector.mult(p.velocity, p.speed);
					p.speed += ACCELERATION;
					p.position.add(displacement);
					p.draw(pGraphics);
					newList.add(p);
				}
			}

			pGraphics.popMatrix();

			this.particles = newList;

		} else {
			this.isDead = true;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.mm.cg.particles.Generator#isFull()
	 */
	@Override
	public boolean isFull() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.mm.cg.particles.Generator#move()
	 */
	/**
	 * Does nothing. This Generator doesn't move.
	 */
	@Override
	public void move() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.mm.cg.particles.Generator#addParticle()
	 */
	/**
	 * Does nothing. This Generator can't add new particles.
	 */
	@Override
	public void addParticle() {
	}

}
